package com.chuangyang.fixboxmaster.ui.fragment.orderprice;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Response;
import com.chuangyang.fixboxlib.widgets.PullDownRefreshListView;
import com.chuangyang.fixboxmaster.R;
import com.chuangyang.fixboxmaster.bean.Brand;
import com.chuangyang.fixboxmaster.bean.BrandInfo;
import com.chuangyang.fixboxmaster.bean.Specification;
import com.chuangyang.fixboxmaster.bean.SpecificationInfo;
import com.chuangyang.fixboxmaster.service.FixBoxApi;
import com.chuangyang.fixboxmaster.service.GsonRequest;
import com.chuangyang.fixboxmaster.ui.ApplianceInfoActivity;
import com.chuangyang.fixboxmaster.ui.adapter.BrandListAdapter;
import com.chuangyang.fixboxmaster.ui.adapter.SpecificationListAdapter;
import com.chuangyang.fixboxmaster.ui.fragment.BaseFragment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

/**
 * Created by Administrator on 2015/6/10.
 */
public class BrandFragment extends BaseFragment implements AdapterView.OnItemClickListener, PullDownRefreshListView.OnRefreshListener {

    private View mContentView;
    private PullDownRefreshListView mBrandListView;
    private String mOrderId;
    private String mBrandID;
    private String mSkillID;
    private ArrayList<Brand> mBrandList = new ArrayList<>();
    private ArrayList<Specification> mSpecificationList = new ArrayList<>();
    private BrandListAdapter mBrandAdapter;
    private SpecificationListAdapter mSpeciAdapter;
    private int mCurrentViewID;
    private static final int VIEW_ID_BRAND = 1;
    private static final int VIEW_ID_SPECIFICATION = 2;
    private String mFilter;

    public static BrandFragment newInstance() {
        return new BrandFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContentView = inflater.inflate(R.layout.fragment_appliance_info, container, false);
        Bundle bundle = getArguments();
        mOrderId = bundle.getString(ApplianceInfoActivity.ORDER_ID);
        mBrandID = bundle.getString(ApplianceInfoActivity.BRAND_ID);
        mSkillID = bundle.getString(ApplianceInfoActivity.SKILL_ID);
        initView();
        return super.onCreateView(inflater, container, savedInstanceState);
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setContentView(mContentView);
        //showResult();
        if (mOrderId != null && !mOrderId.equals("")) {
            mCurrentViewID = VIEW_ID_BRAND;
            loadBrandData();
        } else if (mBrandID != null && !mBrandID.equals("")) {
            mCurrentViewID = VIEW_ID_SPECIFICATION;
            loadSpecificationData();
        }

    }

    private void initView() {
        mBrandListView = (PullDownRefreshListView) mContentView.findViewById(R.id.info_item);
        mBrandListView.setOnItemClickListener(this);
        mBrandAdapter = new BrandListAdapter(mBrandList);
        mSpeciAdapter = new SpecificationListAdapter(mSpecificationList);
        mBrandListView.setOnRefreshListener(this);

    }

    public void loadBrandData() {
        HashMap<String, String> param = new HashMap<>();
        param.put("oid", mOrderId);
        param.put("pageIndex", String.valueOf(mBrandList.size()));

        if (mFilter != null && !mFilter.trim().equals(""))
            param.put("keyword", mFilter.trim());
        addRequest(new GsonRequest<>(FixBoxApi.GET_BRAND, param, BrandInfo.class, responseListener));
    }

    private Response.Listener<BrandInfo> responseListener = new Response.Listener<BrandInfo>() {
        @Override
        public void onResponse(BrandInfo response) {

            if (response != null && response.result != null) {
                mBrandList.addAll(Arrays.asList(response.result.lists));
                showResult();
                if ((mBrandList.size()) >= response.result.totalCount) {
                    Brand brand = new Brand();
                    brand.id = 0;
                    brand.title = "其他";
                    mBrandList.add(brand);
                    mBrandListView.setLoadingEnd();
                } else {
                    mBrandListView.setLoadingIdle();
                }
                mBrandAdapter.setmBrandsTemps(mBrandList);
                mBrandListView.setAdapter(mBrandAdapter);
                mBrandAdapter.notifyDataSetChanged();
            }
        }
    };

    public void loadSpecificationData() {
        HashMap<String, String> param = new HashMap<>();
        if (mBrandID != null)
            param.put("brandId", mBrandID);
        if (mSkillID != null)
            param.put("skillId", mSkillID);
        if (mFilter != null && !mFilter.trim().equals(""))
            param.put("keyword", mFilter.trim());
        param.put("pageIndex", String.valueOf(mSpecificationList.size()));
        addRequest(new GsonRequest<>(FixBoxApi.GET_SPECIFICATION, param, SpecificationInfo.class, responseSpeListener));
    }

    private Response.Listener<SpecificationInfo> responseSpeListener = new Response.Listener<SpecificationInfo>() {
        @Override
        public void onResponse(SpecificationInfo response) {
            if (response != null && response.result != null) {
                mSpecificationList.addAll(Arrays.asList(response.result.lists));
                mBrandListView.setAdapter(mSpeciAdapter);
                mSpeciAdapter.setmBrandsTemps(mSpecificationList);
                mSpeciAdapter.notifyDataSetChanged();
                showResult();
                if (mSpecificationList.size() >= response.result.totalCount)
                    mBrandListView.setLoadingEnd();
                else
                    mBrandListView.setLoadingIdle();
            }
        }
    };

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        if (mCurrentViewID == VIEW_ID_BRAND) {
            Brand brand = mBrandList.get(position);
            bundle.putSerializable("brand", brand);
        } else if (mCurrentViewID == VIEW_ID_SPECIFICATION) {
            Specification specification = mSpecificationList.get(position);
            bundle.putSerializable("specification", specification);
        }
        intent.putExtras(bundle);
        getActivity().setResult(Activity.RESULT_OK, intent);
        getActivity().finish();
    }

    public void updateListView() {

        if (mCurrentViewID == VIEW_ID_BRAND) {
            mBrandList.clear();
            mBrandListView.setAdapter(mBrandAdapter);
            loadBrandData();

        } else if (mCurrentViewID == VIEW_ID_SPECIFICATION) {
            mSpecificationList.clear();
            mBrandListView.setAdapter(mSpeciAdapter);
            loadSpecificationData();
        }
    }

    public void doSearch() {
        if (mCurrentViewID == VIEW_ID_BRAND) {
            mBrandList.clear();
            loadBrandData();
        } else if (mCurrentViewID == VIEW_ID_SPECIFICATION) {
            mSpecificationList.clear();
            mBrandListView.setAdapter(mSpeciAdapter);
            loadSpecificationData();
        }
    }


    public TextWatcher getTextWatcher() {

        return new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mFilter = s.toString();
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Log.e("", "");

            }

            @Override
            public void afterTextChanged(Editable s) {
                Log.e("", "");
            }
        };


    }

    @Override
    public void onRefresh() {
        mBrandListView.setLoading();
        if (mCurrentViewID == VIEW_ID_BRAND) {
            loadBrandData();
        } else if (mCurrentViewID == VIEW_ID_SPECIFICATION) {
            loadSpecificationData();
        }
    }

    @Override
    public ListView getListView() {
        return mBrandListView;
    }
}
